﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.IO;
using System.Globalization;
using System.Web.Compilation;

namespace EntLibContrib.Resource.Web
{
	/// <summary>
	/// WebResourceProviderFactory is a ResourceProviderFactory class which provides a System.Web.Compilation.IResourceProvider
	/// for ASP.NET global and local resources
	/// </summary>
	public class WebResourceProviderFactory : ResourceProviderFactory
	{
		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="WebResourceProviderFactory"/> class.
		/// </summary>
		public WebResourceProviderFactory()
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// When overridden in a derived class, creates a global resource provider.
		/// </summary>
		/// <param name="classKey">The name of the resource class.</param>
		/// <returns>
		/// An <see cref="T:System.Web.Compilation.IResourceProvider"/>.
		/// </returns>
		/// <remarks>The class key is used for the resource manager name in configuration</remarks>
		public override System.Web.Compilation.IResourceProvider CreateGlobalResourceProvider(string classKey)
		{
			return new WebResourceProvider(classKey);
		}

		/// <summary>
		/// When overridden in a derived class, creates a local resource provider.
		/// </summary>
		/// <param name="virtualPath">The path to a resource file.</param>
		/// <returns>
		/// An <see cref="T:System.Web.Compilation.IResourceProvider"/>.
		/// </returns>
		/// <remarks>The name of the web page is used for the resource manager name in configuration</remarks>
		public override System.Web.Compilation.IResourceProvider CreateLocalResourceProvider(string virtualPath)
		{
			string webPageName = Path.GetFileName(virtualPath);
			string resourceManagerName = webPageName.Substring(0, webPageName.Length - Path.GetExtension(webPageName).Length);
			return new WebResourceProvider(resourceManagerName.ToLower(CultureInfo.CurrentCulture));
		}
		#endregion
	}
}
